/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package convert_crypto;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;

/**
 *
 * @author suzunghia
 */
public class ConvertDatabaseGUI extends JPanel implements ActionListener {

    JLabel lb_pathFrom;
    JTextField txt_pathFrom;
    JButton btn_browser1;
    JLabel lb_user;
    JTextField txt_user;
    JLabel lb_pass;
    JPasswordField txt_pass;
    JLabel lb_port;
    JTextField txt_port;
    JLabel lb_pathTo;
    JTextField txt_pathTo;
    JButton btn_browser2;
    JButton btn_Convert;
    JFileChooser fcFrom;
    JFileChooser fcTo;
    JLabel lb_space1;
    JLabel lb_space2;
    public ConvertDatabaseGUI() {
         try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ConvertDatabaseGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
         
        txt_pathFrom = new JTextField("");
        txt_pathFrom.setPreferredSize(new Dimension(200, 30));
        txt_pathTo = new JTextField("");
        txt_pathTo.setPreferredSize(new Dimension(200, 30));
        
        txt_user = new JTextField("");
        txt_user.setPreferredSize(new Dimension(100, 30));
        txt_pass = new JPasswordField("");
        txt_pass.setPreferredSize(new Dimension(100, 30));
        txt_port = new JTextField("");
        txt_port.setPreferredSize(new Dimension(100, 30));
        
        lb_pathFrom = new JLabel("SQL Database path: ");
        lb_space1 = new JLabel(" ");
        lb_space2 = new JLabel(" ");
        lb_pathFrom.setPreferredSize(new Dimension(150, 30));
        lb_pathTo = new JLabel("Custom Database path: ");
        lb_pathTo.setPreferredSize(new Dimension(150, 30));
        lb_user = new JLabel("User: ");
        lb_pass = new JLabel("Pass: ");
        lb_port = new JLabel("Port: ");
        btn_browser1 = new JButton("Browser");
        btn_browser1.addActionListener(this);
        btn_browser2 = new JButton("Browser");
        btn_browser2.addActionListener(this);
        btn_Convert = new JButton("Convert");
        btn_Convert.addActionListener(this);

        fcFrom = new JFileChooser();
        fcTo = new JFileChooser();
    }

    public void showGUI() {
     
        JFrame frame = new JFrame("Convert Database");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setResizable(false);
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        JPanel Head = new JPanel();
        Head.setLayout(new BoxLayout(Head, BoxLayout.Y_AXIS));
        JPanel pathFrom = new JPanel();
        pathFrom.setLayout(new BoxLayout(pathFrom, BoxLayout.X_AXIS));
        pathFrom.add(lb_pathFrom);
        pathFrom.add(txt_pathFrom);
        pathFrom.add(btn_browser1);
        JPanel pathTo = new JPanel();
        pathTo.setLayout(new BoxLayout(pathTo, BoxLayout.X_AXIS));
        pathTo.add(lb_pathTo);
        pathTo.add(txt_pathTo);
        pathTo.add(btn_browser2);

        Head.add(pathFrom);
        Head.add(pathTo);

        JPanel Body = new JPanel();
        Body.setLayout(new BoxLayout(Body, BoxLayout.X_AXIS));
        Body.add(lb_user);
        Body.add(txt_user);
        Body.add(lb_space1);
        Body.add(lb_pass);
        Body.add(txt_pass);
        Body.add(lb_space2);
        Body.add(lb_port);
        Body.add(txt_port);
        
        panel.add(Head);
        panel.add(Body);
        panel.add(btn_Convert);
        btn_Convert.setAlignmentX(CENTER_ALIGNMENT);
        frame.getContentPane().removeAll();
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
        frame.setAlwaysOnTop(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btn_browser1) {
            int returnVal = fcFrom.showOpenDialog(this);
                if(returnVal == JFileChooser.APPROVE_OPTION)
                {
                    txt_pathFrom.setText(fcFrom.getSelectedFile().getPath());
                }
        }
        if (e.getSource() == btn_browser2) {
             int returnVal = fcTo.showOpenDialog(this);
                if(returnVal == JFileChooser.APPROVE_OPTION)
                {
                  txt_pathTo.setText(fcTo.getSelectedFile().getPath());
                }
        }
        if (e.getSource() == btn_Convert){
            ParseDatabase parse = new ParseDatabase();
            File f = new File(txt_pathFrom.getText());
            String str = f.getName();
            int index= str.indexOf(".");
            str = str.substring(0, index);
            if(parse.Connect(txt_port.getText(), str, txt_user.getText(), txt_pass.getText()))
            {
                if(parse.CreateDatabase(str, txt_pathTo.getText()))
                    JOptionPane.showMessageDialog(null, "Success");
                else
                    JOptionPane.showMessageDialog(null, "Fail");
            }
            else
                JOptionPane.showMessageDialog(null, "Can't connect to SQLDatabase");
        }
    }
}
